package cn.iocoder.yudao.module.train.dal.mysql.scanrecord;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.train.dal.dataobject.classsetting.ClassSettingDO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.ScanRecordDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.StudentCoursePerformanceStudentDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.train.dal.dataobject.scanrecord.ScanRecordDO;
import cn.iocoder.yudao.module.train.dal.dataobject.student.StudentDO;
import cn.iocoder.yudao.module.train.dal.dataobject.studentcourseperformance.StudentCoursePerformanceDO;
import cn.iocoder.yudao.module.train.dal.dataobject.venue.VenueDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.train.controller.admin.scanrecord.vo.*;

/**
 * 课程签到记录 Mapper
 *
 * @author waynelam
 */
@Mapper
public interface ScanRecordMapper extends BaseMapperX<ScanRecordDO> {

    default PageResult<ScanRecordDTO> selectPage(ScanRecordPageReqVO reqVO) {
        Page<ScanRecordDTO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        MPJLambdaWrapper<ScanRecordDO> wrapper = new MPJLambdaWrapper<ScanRecordDO>()
                .selectAll(ScanRecordDO.class)
                .select(StudentDO::getStudentName)
                .select(StudentDO::getStudentPhoneNumber)
                .leftJoin(StudentDO.class, StudentDO::getId, ScanRecordDO::getStudentId)
                .select(ClassSettingDO::getVenueId)
                .select(ClassSettingDO::getClassesId)
                .leftJoin(ClassSettingDO.class, ClassSettingDO::getId, ScanRecordDO::getClassSettingId)
                .select(VenueDO::getVenueNo)
                .leftJoin(VenueDO.class, VenueDO::getId, ClassSettingDO::getVenueId)
                .eqIfExists(ScanRecordDO::getStudentId, reqVO.getStudentId())
                .likeIfExists(StudentDO::getStudentName, reqVO.getStudentName())
                .likeIfExists(VenueDO::getVenueNo, reqVO.getVenueNo())
                .eqIfExists(ClassSettingDO::getClassesId, reqVO.getClassesId())
                .eqIfExists(ScanRecordDO::getDeviceId, reqVO.getDeviceId())
                .eqIfExists(ScanRecordDO::getDeviceSn, reqVO.getDeviceSn())
                .eqIfExists(ClassSettingDO::getVenueId, reqVO.getVenueId())
                .orderByDesc(ScanRecordDO::getCreateTime);
        if (reqVO.getCreateTime() != null && reqVO.getCreateTime().length == 2) {
            wrapper.between(OrderDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        IPage<ScanRecordDTO> resultPage = selectJoinPage(page, ScanRecordDTO.class, wrapper);
        return new PageResult<>(resultPage.getRecords(), resultPage.getTotal());
    }

}
